package com.central.modules.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.central.common.page.PageData;
import com.central.common.utils.Result;
import com.central.common.validator.ValidatorUtils;
import com.central.common.validator.group.AddGroup;
import com.central.common.validator.group.DefaultGroup;
import com.central.modules.sys.dto.PageSysDictDataVo;
import com.central.modules.sys.dto.PageSysDictTypeVo;
import com.central.modules.sys.entity.SysDictDataEntity;
import com.central.modules.sys.entity.SysDictTypeEntity;
import com.central.modules.sys.enums.SuperAdminEnum;
import com.central.modules.sys.enums.SysGenderEnum;
import com.central.modules.sys.enums.SysNoticeTypeEnum;
import com.central.modules.sys.enums.UserStatusEnum;
import com.central.modules.sys.model.AddSysDictDataRequest;
import com.central.modules.sys.model.AddSysDictTypeRequest;
import com.central.modules.sys.model.SysDictDataQueryBean;
import com.central.modules.sys.model.SysDictTypeQueryBean;
import com.central.modules.sys.service.SysDictDataService;
import com.central.modules.sys.service.SysDictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@RestController
@RequestMapping("/sys_dict_type")
public class SysDictTypeController {

    private final SysDictTypeService sysDictTypeService;
    private final SysDictDataService sysDictDataService;

    //分页查询可配置字典类型
    @GetMapping("/type/page_search")
    public Result<PageData<PageSysDictTypeVo>> searchSysDictType(SysDictTypeQueryBean request) {
        IPage<SysDictTypeEntity> iPage = sysDictTypeService.searchSysDictTypes(request);
        List<PageSysDictTypeVo> typeVos = iPage.getRecords().stream().map(e -> {
            PageSysDictTypeVo typeVo = new PageSysDictTypeVo();
            BeanUtils.copyProperties(e, typeVo);
            return typeVo;
        }).collect(Collectors.toList());
        return Result.succeed(PageData.copy(iPage, typeVos));
    }

    //删除
    @GetMapping("/type/del")
    public Result<Boolean> delSysDictType(Long id) {
        boolean r = sysDictTypeService.removeSysDictType(id);
        if (r) {
            sysDictDataService.cleanSysDictDataByTypeId(id);
        }
        return Result.succeed(r);
    }

    //添加
    @PostMapping("/type/add")
    public Result<Map<String, Object>> addType(@RequestBody AddSysDictTypeRequest request) {
        ValidatorUtils.validateEntity(request, AddGroup.class, DefaultGroup.class);
        Boolean result = sysDictTypeService.addSysDictType(request);
        return Result.succeedForKey("r", result);
    }


    //数据分页查询
    @GetMapping("/data/page_search")
    public Result<PageData<PageSysDictDataVo>> pageSearchDatas(@RequestBody SysDictDataQueryBean request) {
        IPage<SysDictDataEntity> iPage = sysDictDataService.searchSysDictDatas(request);
        List<PageSysDictDataVo> dataVos = iPage.getRecords().stream().map(e -> {
            PageSysDictDataVo dictDataVo = new PageSysDictDataVo();
            BeanUtils.copyProperties(e, dictDataVo);
            return dictDataVo;
        }).collect(Collectors.toList());
        return Result.succeed(PageData.copy(iPage, dataVos));
    }


    //添加字典下的key+值
    @PostMapping("/data/add")
    public Result<Map<String, Object>> addData(@RequestBody AddSysDictDataRequest request) {
        ValidatorUtils.validateEntity(request, AddGroup.class, DefaultGroup.class);
        return Result.succeedForKey("r", sysDictDataService.addSysDictData(request));
    }

    //删除字典下的key+值
    @GetMapping("/data/del")
    public Result<Map<String, Object>> delData(@RequestParam Long id) {
        Boolean result = sysDictDataService.removeSysDictData(id);
        return Result.succeedForKey("r", result);
    }

    //查询所有的字典
    @GetMapping("/type/all")
    public Result<List<Map<String, Object>>> allTypes() {
        List<Map<String, Object>> allList = new ArrayList<>();
        //系统内部的
        allList.add(SuperAdminEnum.toType());
        allList.add(UserStatusEnum.toType());
        allList.add(SysNoticeTypeEnum.toType());
        allList.add(SysGenderEnum.toType());


        //自定义
        List<Map<String, Object>> c_result = sysDictTypeService.findAll().stream().map(e -> {
            Map<String, Object> typeMap = new HashMap<>();
            typeMap.put("dictType", e.getDictType());
            typeMap.put("dictName", e.getDictName());
            List<SysDictDataEntity> sysDictDataEntities = sysDictDataService.findAllByTypeId(e.getId());
            typeMap.put("datas", sysDictDataEntities.stream().map(d -> {
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("dictLabel", d.getDictLabel());
                dataMap.put("dictValue", d.getDictValue());
                return dataMap;
            }).collect(Collectors.toList()));
            return typeMap;
        }).collect(Collectors.toList());

        allList.addAll(c_result);

        return Result.succeed(allList);
    }


}
